//
//  VolumeItemView.m
//  JiaoX
//
//  Created by Northwan on 15/4/3.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "VolumeItemView.h"

@interface VolumeItemView()
@property(nonatomic,strong)UIImageView *isPastView;
@property(nonatomic,strong)UILabel *amountLable;
@property(nonatomic,strong)UILabel *useAmountLable;
@property(nonatomic,strong)UILabel *timeOutLable;
@end
@implementation VolumeItemView

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UIImage *bg_img = [UIImage imageNamed:@"bg_volume_item"];
        [self setImage:bg_img];
        [self setFrame:CGRectMake(JXPublicInset, JXPublicInset, JXScreenW-JXPublicInset*2, bg_img.size.height)];
        
        //现金券金额
        _amountLable = [[UILabel alloc]init];
        [_amountLable setFont:[UIFont systemFontOfSize:34]];
        [_amountLable setTextColor:[UIColor whiteColor]];
        [self addSubview:_amountLable];
        
        //满x元可用
        _useAmountLable = [[UILabel alloc]init];
        [_useAmountLable setFont:[UIFont systemFontOfSize:15]];
        [_useAmountLable setTextColor:[UIColor whiteColor]];
//        [_useAmountLable setContentMode:UIViewContentModeCenter];此属性无效
        _useAmountLable.textAlignment = UITextAlignmentCenter;
        [self addSubview:_useAmountLable];
        
        //有效期
        _timeOutLable = [[UILabel alloc]init];
        [_timeOutLable setFont:[UIFont systemFontOfSize:15]];
        [_timeOutLable setTextColor:[UIColor whiteColor]];
        [self addSubview:_timeOutLable];
        
        //是否过期的
        _isPastView = [[UIImageView alloc] init];
        UIImage *isPastImg = [UIImage imageNamed:@"icon_past_time"];
        [_isPastView setImage:isPastImg];
        [_isPastView setFrame:CGRectMake(self.width-isPastImg.size.width, 0, isPastImg.size.width, isPastImg.size.height)];
        [self addSubview:_isPastView];
    }
    return self;
}

-(void)setVoucher:(Voucher *)voucher{
    //设置现金券金额
    NSString *amount = [NSString stringWithFormat:@"¥%@",voucher.amount];
    CGSize amountSize = [amount sizeWithFont:[UIFont fontWithName:@"Helvetica-Bold" size:34]];
    
    [_amountLable setFrame:CGRectMake(JXPublicInset, (155/2)*0.5- amountSize.height*0.5, amountSize.width, amountSize.height)];
    [_amountLable setText:amount];
    
    //有效期文字meassure
    NSString *effactiveTime = [NSString stringWithFormat:@"有效期至 %@",voucher.effactiveTime];
    CGSize timeOutSize = [effactiveTime sizeWithFont:[UIFont systemFontOfSize:15]];

    //设置满足x元可用,这里使用一个技巧就是根据这个控件下面的长度来设为本控件的长度，然后设置contentMode为居中
//    CGSize useAmountSize = [voucher.useCondition sizeWithFont:[UIFont systemFontOfSize:14]];
    
    [_useAmountLable setFrame:CGRectMake(self.width-timeOutSize.width-JXPublicInset, CGRectGetMinY(_amountLable.frame), timeOutSize.width, timeOutSize.height)];
    [_useAmountLable setText:voucher.useCondition];
    
    //设置有效期
    [_timeOutLable setFrame:CGRectMake(self.width-timeOutSize.width-JXPublicInset, CGRectGetMaxY(_useAmountLable.frame)+JXPublicInset, timeOutSize.width, timeOutSize.height)];
    [_timeOutLable setText:effactiveTime];
    
    //设置是否过期
    if(voucher.isPast == 1){//已过期
        [_isPastView setHidden:NO];
    }else{//未过期
        [_isPastView setHidden:YES];
    }
}

@end
